package com.itheima.controller;

import com.itheima.pojo.Article;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @PostMapping   /*  添加文章  */
    public Result add(@RequestBody @Validated(value = Article.Add.class) Article article) {
        articleService.add(article);
        return Result.success();
    }

    @GetMapping   /*  获取所有文章（带分页过滤）  */
    public Result<PageBean<Article>> list(
            Integer pageNum,Integer pageSize,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String state
    ) {
        PageBean<Article> pb = articleService.list(pageNum,pageSize,categoryId,state);
        return Result.success(pb);
    }

    @GetMapping("/detail")  /*  获取文章详情  */
    public Result<Article> findById(@RequestParam Integer id) {
        Article article = articleService.findById(id);
        return Result.success(article);
    }

    @PutMapping    /*  修改文章  */
    public Result update(@RequestBody @Validated(value = Article.Update.class) Article article) {
        articleService.update(article);
        return Result.success();
    }

    @DeleteMapping  /*  删除文章  */
    public Result delete(@RequestParam Integer id) {
        articleService.delete(id);
        return Result.success();
    }
}
